package com.czk

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/6 9:56
 */
object Spark_WordCount {
  def main(args: Array[String]): Unit = {
    // spark框架
    // 建立连接
    val sparkConf = new SparkConf().setMaster("local").setAppName("WordCunt")
    val sc = new SparkContext(sparkConf)
    // 执行业务操作
    // 读取文件
    val lines: RDD[String] = sc.textFile("F:\\IdeaProjects\\bigData\\spark\\datas\\test.txt")
    // 扁平化操作
    val words: RDD[String] = lines.flatMap(_.split(" "))
    // 分组
    val wordGroup: RDD[(String, Iterable[String])] = words.groupBy(word => word)
    //对分组后的数据进行转换
    val worToCount=wordGroup.map {
      case (word, list) => {
        (word, list.size)
      }
    }
    println(worToCount)
    sc.stop()
  }
}
